--[[
    类似 UICheckBoxButtonGroup 所包含的UIButton互斥
    args存放的是UIPushButton
]]

local RadioButton = class("RadioButton");
function RadioButton:ctor(args)
    self.buttons = args;
    self.handler = nil;
    self.curIndex = 0;

    for key, var in pairs(self.buttons) do
        var:onButtonClicked(function()
            self:setSelectIndex(key);
        end);
    end
    
end

function RadioButton:setClickHandler(handler)
    self.handler = handler;
end

function RadioButton:setSelectIndex(index)
    if index > table.nums(self.buttons) then
        return;
    end
    if self.curIndex ~= index then
        self.curIndex = index;
        for key, var in pairs(self.buttons) do
            local isSelected = key == index;
            if isSelected then
                var:setButtonEnabled(false);
                var:setHighlighted(true);
            else
                var:setButtonEnabled(true);
                var:setHighlighted(false);
            end
        end
        if self.handler then
            self.handler(index);
        end
    end
end

function RadioButton:getCurIndex()
    return self.curIndex;
end

function RadioButton:getCurBtn()
    return self.buttons[self.curIndex];
end

return RadioButton;

--endregion
